package benchmark;

import benchmark.entity.CallBindInfo;
import benchmark.entity.DirectCallResponse;
import benchmark.util.JsonHelper;
import org.springframework.http.HttpEntity;
import org.springframework.http.HttpHeaders;
import org.springframework.http.MediaType;
import org.springframework.web.client.RestTemplate;

import java.nio.charset.Charset;
import java.util.Collections;
import java.util.concurrent.CountDownLatch;

/**
 * @Author: admin
 * @Description:
 * @Date: 2018/10/16 15:51
 */

public class UserThread implements Runnable {
    private String name;
    private CountDownLatch latch;

    public UserThread(String name, CountDownLatch latch) {
        this.name = name;
        this.latch = latch;
    }

    public static void sendRequest() {
        RestTemplate restTemplate = new RestTemplate();
        CallBindInfo callBindInfo = new CallBindInfo("18631586097", "15231538923", "MD-18-230293");
        //DirectCallInfo callBindInfo = new DirectCallInfo("15176530589", "15231538923", null);
        HttpHeaders httpHeaders = new HttpHeaders();
        httpHeaders.add("token","02c596e1-9d8e-45bb-964d-2d41e9c4fa19");
        //设置HTTP请求的请求头信息
        httpHeaders.setContentType(MediaType.parseMediaType("application/json;charset=UTF-8"));
        //设置相应内容，相应内容将被转换为json格式返回
        httpHeaders.setAcceptCharset(Collections.singletonList(Charset.forName("UTF-8")));
        //httpHeaders.setAccept(Collections.singletonList(MediaType.APPLICATION_JSON));
        //设置HttpEntity的Body类型为String，调用StringHttpMessageConverter转换报文体参数
        HttpEntity httpEntity = new HttpEntity(JsonHelper.objectToJson(callBindInfo), httpHeaders);

        long startTime = System.currentTimeMillis();

        Object res= restTemplate.getForObject("http://192.168.10.129:5055/rpop",String.class);
        System.out.println(res);

//        DirectCallResponse resModel = restTemplate.postForObject(
//                "https://househiddencall.allhome.com.cn/platformHouseHiddenCall/miniapp/calls/directCall",
//                //"http://192.168.0.201:7091/platformBaseHiddenCall/business/calls/directCall",
//                //"https://hiddencall.allhome.com.cn/platformBaseHiddenCall/business/calls/directCall",
//                httpEntity, DirectCallResponse.class);
//
////        DirectCallResponse resModel = restTemplate.postForObject(
////                //"http://192.168.0.201:7092/platformHouseHiddenCall/miniapp/calls/directCall",
////                //"http://192.168.0.201:7091/platformBaseHiddenCall/business/calls/directCall",
////                "https://hiddencall.allhome.com.cn/platformBaseHiddenCall/business/calls/directCall",
////                httpEntity, ResultVO.class).getResult();
//        long endTime = System.currentTimeMillis();//记录结束时间
//        float excTime = (float) (endTime - startTime) / 1000;
//        System.out.println("--------" + resModel.getCallId() + "-------" + excTime);
    }

    public void run() {
        try {
            latch.await();
        } catch (InterruptedException e) {
            e.printStackTrace();
        }
        System.out.println(name + ": start");
        try {
            for (int i = 0; i < 5; i++) {
                UserThread.sendRequest();
                Thread.sleep(1);
            }
        } catch (InterruptedException e) {
            e.printStackTrace();
        }

        System.out.println(name + ": end");
    }
}
